import { Config } from "../../src/config/Config"
import { DbConfig } from "../../src/config/DbConfig"
import { MysqlClient } from "../../src/engine/utils/MySqlClient"
import { MysqlManager } from "../../src/engine/utils/MysqlManager"

export class DailiDao {

    private static getDbName() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     return DbConfig.Db_Name.db_yuxiaxie2
        // } 
        // else if (Config.environment == Config.EnvironmentType.xj) {
        //     return DbConfig.Db_Name.db_yuxiaxie2
        // }
        // else if(Config.environment == Config.EnvironmentType.Jk){
        //     return DbConfig.Db_Name.db_yuxiaxie3
        // }
        // else if(Config.environment == Config.EnvironmentType.chaoyou){
        //     return DbConfig.Db_Name.db_yuxiaxie_chaoyu
        // }
        // else {
            return DbConfig.Db_Name.db_laoyou
        // }
    }


    public static async get_daili_gems(username: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select  * from  t_daili  where username = ? "
        let sqlParams = [username];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return results[0]["gems"]
        } else {
            return 0
        }
    }

    public static async get_daili() {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select  * from  t_daili  "
        let sqlParams = [];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return results
        } else {
            return []
        }
    }

    public static async charge_to_daili(id:number, username:string, num:number){
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let gems:number = await this.get_daili_gems(username)
        gems += num
        let sql = "update " + this.getDbName() + ".t_daili" + " set `gems` =?   where `username`=?";
        let sqlParams = [gems, username];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }
}